//
//  SSSearchController.m
//  textDemo
//
//  Created by Esther on 2017/2/20.
//  Copyright © 2017年 Esther. All rights reserved.
//

#import "SSSearchController.h"
#import "SSGoodsListController.h"

@interface SSSearchController ()<UISearchBarDelegate>
@property(nonatomic, strong) UISearchBar *search;
@end

@implementation SSSearchController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self backItem];
    
    self.navigationItem.titleView = self.search;

    
}

- (void)backItem{
    UIButton *back = [UIButton buttonWithType:(UIButtonTypeCustom)];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithCustomView:back];
    self.navigationItem.leftBarButtonItem = backItem;
}

- (void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];
    [_search becomeFirstResponder];
    
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];

}

#pragma mark -
#pragma mark - searchBarDelegate
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    
     [searchBar resignFirstResponder];
    [self.navigationController popViewControllerAnimated:YES];
   
    
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    
   SSGoodsListController *goodsList = [[SSGoodsListController alloc] init];
    //解决页面跳转时闪烁问题，如果键盘存在的话，页面跳转的动画会出现两次的push，但nav的子视图中并没有出现多余的viewcontroller
    
    
    [searchBar resignFirstResponder];
    [self.navigationController pushViewController:goodsList animated:YES];
    
    
}


#pragma mark -
#pragma mark - 懒加载
- (UISearchBar *)search{
    if (!_search) {
        _search = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, [UIScreen ss_screenWidth] - 40, 25)];
        _search.placeholder = @"商品名 / 功效 / 品牌";
        [_search becomeFirstResponder];
         _search.returnKeyType = UIReturnKeySearch;
        _search.showsCancelButton = YES;
        _search.delegate = self;
    }
    return _search;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
